package com.example.project.projectspringcloud.config;

import com.example.project.projectspringcloud.filter.ProjectFilter;
import com.example.project.projectspringcloud.intercepter.ProjectIntercepter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import java.util.Locale;

/**
 * 系统主配置器
 */
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {

    @Bean
    public FilterRegistrationBean filterRegistration() {
        FilterRegistrationBean frb = new FilterRegistrationBean();
        frb.setFilter(new ProjectFilter());
        return frb;
    }

    @Bean
    public ProjectIntercepter projectIntercepter() {
        return new ProjectIntercepter();
    }

    /**
     * project interceptors mapping relation
     *
     * @param registry
     */
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(projectIntercepter()).addPathPatterns("/**");

        LocaleChangeInterceptor localeInterceptor = new LocaleChangeInterceptor();
        localeInterceptor.setParamName("lang");
        registry.addInterceptor(localeInterceptor);
    }

    /**
     * no controller URL mapping relation
     *
     * @param registry
     */
        @Override
        protected void addViewControllers(ViewControllerRegistry registry) {
        /*Spring MVC 中提供了一个方法，可以把类似代码统一管理，减少类似代码的书写（根据项目要求，或者代码规范，不一定非要统一管理页面跳转，有时会把相同业务逻辑的代码放在一个类中）。
        在继承WebMvcConfigurerAdapter的DemoMVCConfig类中重载addViewControllers*/
        // 管理员登录
        registry.addViewController("/admin/login").setViewName("admin/login");
        // 用户登录
        // registry.addViewController("/login").setViewName("login");
        // 用户注册
        // registry.addViewController("/register").setViewName("register");
        // 用户密码找回
        // registry.addViewController("/retrieve").setViewName("retrieve");
    }

    /**
     * reources that do not pass the interceptor
     *
     * @param registry
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 静态文件
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        // kindeditor插件引用
        registry.addResourceHandler("/kindeditor/**").addResourceLocations("classpath:/kindeditor/");
        // swagger2页面
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    /**
     * 默认解析器 其中locale表示默认语言
     */
    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver localeResolver = new SessionLocaleResolver();
        localeResolver.setDefaultLocale(Locale.CHINA);
        return localeResolver;
    }

}
